abstract class Shape {
  x: number;
  y: number;
  // type:string;
  fillStyle: string;
  zIndex: number;
  type: string | undefined;
  offsetX: number = 0;
  offsetY: number = 0;
  constructor(x: number, y: number, fillStyle: string, zIndex: number) {
    this.x = x;
    this.y = y;
    this.fillStyle = fillStyle;
    this.zIndex = zIndex;
  }
  setType(type: string) {
    this.type = type;
  }
  abstract draw(ctx: CanvasRenderingContext2D): void;
  abstract isHovered(x: number, y: number): boolean;
  setOffset(x: number, y: number) {
    this.offsetX = x - this.x;
    this.offsetY = y - this.y;
  }
  setPosition(x: number, y: number) {
    this.x = x - this.offsetX;
    this.y = y - this.offsetY;
  }
  clearCanvas(ctx: CanvasRenderingContext2D) {
    ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height);
  }
}
interface Point {
  x: number;
  y: number;
}
//圆型
class Circle extends Shape {
  isHovered(x: number, y: number): boolean {
    // throw new Error("Method not implemented.");
    // 勾股定理
    return (
      Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2) <=
      Math.pow(this.radius, 2)
    );
  }
  radius: number;
  constructor(
    x: number,
    y: number,
    radius: number,
    fillStyle: string,
    zIndex: number = 0,
  ) {
    super(x, y, fillStyle, zIndex);
    super.setType('circle');
    this.radius = radius;
  }
  draw(ctx: CanvasRenderingContext2D): void {
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
    ctx.closePath();
    ctx.fillStyle = this.fillStyle;
    ctx.fill();
    // throw new Error("Method not implemented.");
  }
}
//矩形
class Rectangle extends Shape {
  isHovered(x: number, y: number): boolean {
    return (
      this.x <= x &&
      this.x + this.width >= x &&
      this.y <= y &&
      this.y + this.height >= y
    );
  }
  width: number;
  height: number;
  constructor(
    x: number,
    y: number,
    width: number,
    height: number,
    fillStyle: string,
    zIndex: number,
  ) {
    super(x, y, fillStyle, zIndex);
    super.setType('rectangle');
    this.width = width;
    this.height = height;
  }
  draw(ctx: CanvasRenderingContext2D): void {
    // throw new Error("Method not implemented.");
    ctx.beginPath();
    ctx.rect(this.x, this.y, this.width, this.height);
    ctx.closePath();
    ctx.fillStyle = this.fillStyle;
    ctx.fill();
  }
}
// 三角形
class Triangle extends Shape {
  isHovered(x: number, y: number): boolean {
    let result = false;
    if (
      this.createLineFunctionByPoint(this.pointList[0], this.pointList[1])(x) <=
        y &&
      this.createLineFunctionByPoint(this.pointList[0], this.pointList[2])(x) <=
        y &&
      this.createLineFunctionByPoint(this.pointList[1], this.pointList[2])(x) >=
        y
    ) {
      result = true;
    }
    console.log(`是否在三角形里面：${result}`)
    return result;
  }
  side: number;
  pointList: Point[] = [];
  constructor(
    x: number,
    y: number,
    side: number,
    fillStyle: string,
    zIndex: number,
  ) {
    super(x, y, fillStyle, zIndex);
    super.setType('triangle');
    this.side = side;
    this.computePoint();
  }
  draw(ctx: CanvasRenderingContext2D): void {
    ctx.beginPath();
    ctx.moveTo(this.pointList[0].x, this.pointList[0].y);
    ctx.lineTo(this.pointList[1].x, this.pointList[1].y);
    ctx.lineTo(this.pointList[2].x, this.pointList[2].y);
    ctx.closePath();
    ctx.fillStyle = this.fillStyle;
    ctx.fill();
    // throw new Error("Method not implemented.");
  }

  // 传入不同的点，生成这条直线的函数
  createLineFunctionByPoint(firstPoint: Point, lastPoint: Point) {
    return (x: number) =>
      ((firstPoint.y - lastPoint.y) / (firstPoint.x - lastPoint.x)) * x +
      firstPoint.y -
      ((firstPoint.y - lastPoint.y) / (firstPoint.x - lastPoint.x)) *
        firstPoint.x;
  }
  setPosition(x: number, y: number) {
    super.setPosition(x, y);
    console.log("执行三角形的setPosition了")
    this.computePoint();
  }
  computePoint() {
    this.pointList = []
    this.pointList.push({
      x: this.x,
      y: this.y - this.side / Math.pow(3, 1 / 2),
    });
    this.pointList.push({
      x: this.x + this.side / 2,
      y: this.y + this.side / Math.pow(3, 1 / 2) / 2,
    });
    this.pointList.push({
      x: this.x - this.side / 2,
      y: this.y + this.side / Math.pow(3, 1 / 2) / 2,
    });
    // console.log("重新计算了三角形的顶点")
  }
}
export { Circle, Rectangle, Triangle, Shape };
